package com.mhkj.fruit.key.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.fruit.key.entity.KeyFruitArchives;
import com.mhkj.fruit.key.service.KeyFruitArchivesService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 成果档案
 */

@Controller
@RequestMapping("/key/fruit/archives")
public class KeyFruitArchivesController extends BaseController<KeyFruitArchives> {
	@Autowired
	private KeyFruitArchivesService keyFruitArchivesService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private DictionaryService dictionaryService;
	
	/**
	 * 分页查询成果档案
	 * 
	 */
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<KeyFruitArchives> listByPage( KeyFruitArchives KeyFruitArchives, int pageIndex, int pageSize ) {
		String code = KeyFruitArchives.getCode();
		if(StringUtils.isEmpty(code)) {
			return null;
		}
		Dictionary byCode = dictionaryService.getByCode(code);
		KeyFruitArchives.setKeyType(byCode);
		Pagination<KeyFruitArchives> pagination = keyFruitArchivesService.listByPage(KeyFruitArchives,pageIndex, pageSize);
		List<KeyFruitArchives> data = pagination.getData();
		for (KeyFruitArchives kfa : data) {
			Employee employeeInfo = orgService.employeeInfo(kfa.getCreateBy());
			kfa.setAuthorName(employeeInfo.getName());
			kfa.setBusiness(kfa.getPlanNode().getBusiness().getTypeName());
			kfa.setPlanNodeName(kfa.getPlanNode().getNodeName());
//			kfa.setFruitTypeName(kfa.getFruitType().getNodePrepare().getNodeName());
			kfa.setPlanNodeId(kfa.getPlanNode().getId());
			kfa.setPlanName(kfa.getPlanNode().getPlan().getName());
		}
		
		return pagination;
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(KeyFruitArchives KeyFruitArchives) {
//		 
//			Dictionary byCode = dictionaryService.getByCode(KeyFruitArchives.getCode());
//			KeyFruitArchives.setKeyType(byCode);
//			FruitType fruitType = fruittypeservice.getById(46);
//			KeyFruitArchives.setFruitType(fruitType);
//			PlanNode planNode = planNodeService.getById(258);
//			KeyFruitArchives.setPlanNode(planNode);
//			PlanFeedBack planFeedBack =PlanFeedBackService.getById(6);
//			KeyFruitArchives.setPlanFeedBack(planFeedBack);
		
		String message = keyFruitArchivesService.save(KeyFruitArchives);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(KeyFruitArchives KeyFruitArchives) {
		 
		String message = keyFruitArchivesService.save(KeyFruitArchives);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del",method={RequestMethod.POST})
	@ResponseBody
	public ResponseMessage del(Long id) {
		if(StringUtils.isEmpty(id)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "id为空!");
		}
		KeyFruitArchives KeyFruitArchives = new KeyFruitArchives();
		KeyFruitArchives.setId(id);
		String message = keyFruitArchivesService.del(KeyFruitArchives);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

 
	/**
	 * 查询成果档案信息
	 */
	@RequestMapping(value="/info", method=RequestMethod.POST)
	@ResponseBody
	public KeyFruitArchives info(@RequestParam(value = "id", required = true) long id) {
		KeyFruitArchives KeyFruitArchives = keyFruitArchivesService.getById(id);
		return KeyFruitArchives;
	}


	/**
	 * 批量删除成果档案
	 */
	@RequestMapping(value="/delAll", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delAll(@RequestParam(value="id",required=true) String id) {
		String[] split = id.split(",");
		for (String str : split) {
			long parseLong = Long.parseLong(str);
			KeyFruitArchives KeyFruitArchives = keyFruitArchivesService.getById(parseLong);
			keyFruitArchivesService.del(KeyFruitArchives);
		}
		
		return ResponseMessage.createMessage(true, "ok");
	}
 
 
	}
